# 作者: 薛文寅仟
# 2025年01月02日21时46分49秒
# 5443912@qq.com

my_list = "This is a test string form Andrew".split()
print(my_list)
print("-" * 50)


def change_lower(str_name):
    """
    回调函数，比较的时候把它转成小写去比较，
    不会把结果中的大写转成小写
    """
    return str_name.lower()


# key传递一个比较规则的函数，比较规则发生了改变
# sort和sorted的区别在于sort会改变原来的列表，是原地排序，而sorted不会
# sorted返回一个新的排序列表
print(sorted(my_list, key=change_lower))
print(my_list)

my_list.sort(key=change_lower)
print(my_list)
print("-" * 50)

# 竖着选：alt+shift+鼠标
student_tuples = [
    ('jane', 'B', 12),
    ('john', 'A', 15),
    ('dave', 'B', 10),
]


def func(x):
    """
    lambda表达式就是匿名函数，等价于这个函数
    """
    return x[2]


print(sorted(student_tuples, key=lambda x: x[2]))
print("-" * 50)


class Student:
    def __init__(self, name, grade, age):
        self.name = name
        self.grade = grade
        self.age = age

    def __repr__(self):
        """
        相对于str来说更方便，可以返回非字符串类型
        """
        return repr((self.name, self.grade, self.age))


student = Student('john', 'A', 15)
print(student)
print("-" * 50)

student_objects = [
    Student('jane', 'B', 12),
    Student('john', 'A', 15),
    Student('dave', 'B', 10),
]
print(sorted(student_objects, key=lambda student: student.name))
print("-" * 50)
print(sorted(student_objects, key=lambda student: student.age))
print("-" * 50)
print(sorted(student_objects, key=lambda student: student.grade))
print("-" * 50)

from operator import itemgetter, attrgetter

# itemgetter争对元组和列表，你去取第几个，对字典来说写的是键，attrgetter去取属性，是对象用的
print('使用operator系列')
print(sorted(student_tuples, key=itemgetter(0)))  # 0,1,2分别对应第一列，第二列，第三列
print("-" * 50)
print(sorted(student_objects, key=attrgetter('name')))  # 按属性去排
print("-" * 50)
print('使用operator系列，多列排序')
print(sorted(student_tuples, key=itemgetter(1, 2)))  # 先按第二列排，在第二列有序的情况下再按第三列排
print("-" * 50)
print(sorted(student_tuples, key=lambda x: (x[1], x[2])))  # lambda表达式的写法
print("-" * 50)
print(sorted(student_objects, key=attrgetter('grade', 'age')))  # 先按grade,再按age
print("-" * 50)
print(sorted(student_tuples, key=lambda x: (x[1], -x[2])))  # 第二列升序，第三列降序，在第三列前面加个负号就行
print("-" * 50)
print(sorted(student_objects, key=attrgetter('grade', 'age'), reverse=True))  # reverse=True即是降序
print("-" * 50)
print('查看排序稳定性')
data = [('red', 1), ('blue', 1), ('red', 2), ('blue', 2)]
print(sorted(data, key=itemgetter(0)))
print("-" * 50)
print("字典中混合列表")
my_dict = {'Li': ['M', 7],
           'Zhang': ['E', 2],
           'Wang': ['P', 3],
           'Du': ['C', 2],
           'Ma': ['C', 9],
           'Zhe': ['H', 7]}
# for i in my_dict.items():
#     print(i)
print(sorted(my_dict.items(), key=lambda x: x[1][1]))
print("-" * 50)
print("列表中混合字典")
gameresult = [
    {"name": "Bob", "wins": 10, "losses": 3, "rating": 75.00},
    {"name": "David", "wins": 3, "losses": 5, "rating": 57.00},
    {"name": "Carol", "wins": 4, "losses": 5, "rating": 57.00},
    {"name": "Patty", "wins": 9, "losses": 3, "rating": 71.48}]
print(sorted(gameresult, key=lambda x: x["rating"]))
print("-" * 50)
print(sorted(gameresult, key=itemgetter("rating", "name")))
